package com.telit.common.service;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.push.model.v20160801.PushMessageToAndroidRequest;
import com.aliyuncs.push.model.v20160801.PushMessageToAndroidResponse;
import com.aliyuncs.push.model.v20160801.PushRequest;
import com.aliyuncs.push.model.v20160801.PushResponse;
import com.aliyuncs.utils.ParameterHelper;
import com.telit.common.config.PushMessageProperties;
import com.telit.common.pojo.AliPushMessagePojo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class AliPushMessageService {

    @Autowired
    private PushMessageProperties pushMessageProperties;

    @Autowired
    private DefaultAcsClient defaultAcsClient;

    /**
     * 推送消息给android
     * <p>
     * 参见文档 https://help.aliyun.com/document_detail/48085.html
     */
    public PushMessageToAndroidResponse pushSimpleMessageToAndroid(String message,String pushType,String target,String id) throws Exception {
        PushMessageToAndroidRequest androidRequest = new PushMessageToAndroidRequest();
        //安全性比较高的内容建议使用HTTPS
        androidRequest.setProtocol(ProtocolType.HTTPS);
        //内容较大的请求，使用POST请求
        androidRequest.setMethod(MethodType.POST);
        androidRequest.setAppKey(Long.parseLong(pushMessageProperties.getAppKey()));
        androidRequest.setTarget(target);
        if("ALL".equals(target)){
            androidRequest.setTargetValue("ALL");
        }else {
            androidRequest.setTargetValue(id); //根据Target来设定，如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制)
        }
        androidRequest.setTitle(pushType);
        androidRequest.setBody(message);
        return defaultAcsClient.getAcsResponse(androidRequest);
    }

    /**
     * 新增自定义高级推送接口
     * <p>
     * 参见文档 https://help.aliyun.com/document_detail/48089.html
     * //
     */
    public PushResponse customizeAdvancedPush(AliPushMessagePojo pojo) throws Exception {
        PushRequest pushRequest = new PushRequest();
        //安全性比较高的内容建议使用HTTPS
        pushRequest.setProtocol(ProtocolType.HTTPS);
        //内容较大的请求，使用POST请求
        pushRequest.setMethod(MethodType.POST);
        // 推送目标
        pushRequest.setAppKey(Long.parseLong(pushMessageProperties.getAppKey()));
        pushRequest.setTarget(pojo.getTarget()); //推送目标: DEVICE:按设备推送 ALIAS : 按别名推送 ACCOUNT:按帐号推送  TAG:按标签推送; ALL: 广播推送
        if("ALL".equals(pojo.getTarget())){
            pushRequest.setTargetValue("ALL");
        }else {
            pushRequest.setTargetValue(StringUtils.join(pojo.getAccountList(), ",")); //根据Target来设定，如Target=DEVICE, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制)
        }
        pushRequest.setPushType(pojo.getPushType()); // 消息类型 MESSAGE NOTICE
        pushRequest.setDeviceType(pojo.getDeviceType()); // 设备类型 ANDROID iOS ALL

        // 推送配置
        pushRequest.setTitle(pojo.getTitle());
        pushRequest.setBody(JSON.toJSONString(pojo.getPushContentPojo())); // 消息的内容

        // 推送控制
        String expireSetTime = ParameterHelper.getISO8601Time(new Date(System.currentTimeMillis() + Integer.parseInt(pushMessageProperties.getExpireTime()) * 3600 * 1000)); // 12小时后消息失效, 不会再发送
        pushRequest.setExpireTime(expireSetTime);
        pushRequest.setStoreOffline(true); // 离线消息是否保存,若保存, 在推送时候，用户即使不在线，下一次上线则会收到
        return defaultAcsClient.getAcsResponse(pushRequest);
    }
}
